# -*- coding: utf-8 -*-            
# @Author : chenliang
# @Time : 2023/12/19 15:06

from Case.python_ui.Common.basepage import Basepage
from selenium.common.exceptions import StaleElementReferenceException
import time, pytest
from Case.python_ui.PageLocate.organization_locator import OrganizationLocator
from Case.python_ui.PageLocate.user_orders_locator import OrdersLocator
from Case.python_ui.Common.commontool import CommonTool
from Case.python_ui.Test_data.usergroup_data import UserGroup
import re
from datetime import datetime
from Case.python_ui.Test_data.menu_name_data import MenuName
from Case.python_ui.Common.excel import excel
from selenium.common.exceptions import NoSuchElementException


class UserOrdersPage(Basepage):

    # TL609181 | 个人用户和没有approval权限的子用户进入order界面默认显示
    def enter_orders_page(self):
        time.sleep(2)
        doc = "进入orders菜单页面"
        total_items = 0
        order_id = ''
        result_order_id = ''
        self.wait_elevisble(OrdersLocator.orders_tab, doc)
        self.click_element(OrdersLocator.orders_tab, doc)
        try:
            self.wait_elevisble(OrdersLocator.orders_total_items, doc)
            total_items = self.get_element(OrdersLocator.orders_total_items, doc).text
            time.sleep(5)
            self.wait_elevisble(OrdersLocator.orders_select_num1, doc)
            order_id = self.get_element(OrdersLocator.orders_select_num1, doc).text
            print(order_id)
            self.move_mouse_to_click_element(OrdersLocator.orders_select_num1, doc)
            time.sleep(5)
            result_order_id = self.get_element(OrdersLocator.order_check_order, doc).text
            assert order_id == result_order_id
            self.wait_elevisble(OrdersLocator.order_return, doc)
            self.click_element(OrdersLocator.order_return, doc)
            time.sleep(5)
            self.wait_elevisble(OrdersLocator.orders_view_detail, doc)
            self.click_element(OrdersLocator.orders_view_detail, doc)
            assert order_id == result_order_id

            return total_items
        except:
            return total_items

    # TL609190 | Order界面搜索功能校验
    def select_id(self):
        time.sleep(2)
        doc = "进入orders页面查询id"
        self.wait_elevisble(OrdersLocator.orders_tab, doc)
        self.click_element(OrdersLocator.orders_tab, doc)
        self.wait_elevisble(OrdersLocator.order_select_terms, doc)
        self.move_mouse_to_click_element(OrdersLocator.order_select_terms, doc)
        self.wait_elevisble(OrdersLocator.term_product_name, doc)
        self.click_element(OrdersLocator.term_product_name, doc)
        time.sleep(5)
        self.wait_elevisble(OrdersLocator.term_product_name1, doc)
        text1 = self.get_element(OrdersLocator.term_product_name1, doc).text
        assert text1 == 'Search by product name'

    # TL609193 | Order界面筛选、时间框功能校验
    def select_conditions(self):
        doc = "Order界面筛选、时间框功能校验"
        self.wait_elevisble(OrdersLocator.orders_tab, doc)
        self.click_element(OrdersLocator.orders_tab, doc)
        self.wait_elevisble(OrdersLocator.order_time, doc)
        self.click_element(OrdersLocator.order_time, doc)
        self.wait_elevisble(OrdersLocator.order_select_time, doc)
        self.click_element(OrdersLocator.order_select_time, doc)
        self.click_element(OrdersLocator.order_select_time, doc)
        time.sleep(2)
        # self.wait_elevisble(OrdersLocator.order_time_clear, doc)
        self.move_mouse_to_click_element(OrdersLocator.order_time_clear, doc)
        # 根据Order_id查询数据
        # self.wait_elevisble(OrdersLocator.orders_total_items, doc)
        # total_items = self.get_element(OrdersLocator.orders_total_items, doc).text
        time.sleep(2)
        # self.wait_elevisble(OrdersLocator.orders_select_num1, doc)
        order_id = self.get_element(OrdersLocator.orders_select_num1, doc).text
        # self.wait_elevisble(OrdersLocator.order_id_input, doc)
        self.send_keys(OrdersLocator.order_id_input, order_id, doc)
        self.click_element(OrdersLocator.order_search, doc)
        time.sleep(2)
        self.wait_elevisble(OrdersLocator.orders_total_items, doc)
        total_items = self.get_element(OrdersLocator.orders_total_items, doc).text
        total = re.findall('\d', total_items)
        assert total[0] == '1'

    # # TL609184 | 有approval权限的子用户和主用户进入order界面默认显示
    def select_conditions(self):
        doc = "Order界面默认显示"
        self.wait_elevisble(OrdersLocator.orders_tab, doc)
        self.click_element(OrdersLocator.orders_tab, doc)
        try:
            self.wait_elevisble(OrdersLocator.orders_select_num1, doc)
            order_id = self.get_element(OrdersLocator.orders_select_num1, doc).text
            print(order_id)
            self.move_mouse_to_click_element(OrdersLocator.orders_select_num1, doc)
            time.sleep(5)
            result_order_id = self.get_element(OrdersLocator.order_check_order, doc).text
            assert order_id == result_order_id
        except:
            print('列表无数据')

    def get_prod_name_to_click(self, prod_name):
        doc = '获取产品名称然后点击'
        prod_list = self.get_elements(OrdersLocator.product, doc)
        time.sleep(2)
        for prod in prod_list:
            if prod.text == prod_name:
                prod.click()
        time.sleep(2)

    def login_order_page(self):
        doc = '打开order页面'
        for i in range(3):
            try:
                self.wait_elevisble(OrdersLocator.orders_tab, doc)
                self.click_element(OrdersLocator.orders_tab, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)

    # product筛选
    def select_product(self, product,if_choose=True):
        doc = "筛选product"
        self.login_order_page()
        for i in range(3):
            try:
                self.wait_elevisble(OrdersLocator.select_product, doc)
                self.move_mouse_to_click_element(OrdersLocator.select_product, doc)
                time.sleep(5)
                if_disabled = self.is_element_disabled(OrdersLocator.reset)
                print(if_disabled)
                break
            except NoSuchElementException:
                continue
        if if_disabled:
            self.move_mouse_to_click_element(OrdersLocator.reset,doc)
        time.sleep(2)
        if if_choose==True:
            self.wait_elevisble(OrdersLocator.product, doc)
            product_list = self.get_elements(OrdersLocator.product, doc)
            for i in product_list:
                if i.text == product:
                    i.click()
                    break
            time.sleep(2)
            self.wait_elevisble(OrdersLocator.product_ok, doc)
            self.click_element(OrdersLocator.product_ok, doc)
            time.sleep(5)
            select_list = []
            select_list = self.get_elements(OrdersLocator.product_list, doc)
            if len(select_list) > 0:
                for i in select_list:
                    assert i.text == product
        time.sleep(2)

    # 清空Order Type 1，Order Status 2，Billing Method 3
    def clear_order_type(self,num,product='pp',if_choose=False):
        doc = "清空Order Type 1，Order Status 2，Billing Method 3"
        if_disabled = False
        if num==1:
            addr = OrdersLocator.select_order_type
        if num==2:
            addr = OrdersLocator.select_order_status
        if num==3:
            addr = OrdersLocator.select_billing_method
        for i in range(3):
            try:
                self.wait_elevisble(addr, doc)
                self.move_mouse_to_click_element(addr, doc)
                time.sleep(5)
                if_disabled = self.is_element_disabled(OrdersLocator.reset)
                print(if_disabled)
                break
            except NoSuchElementException:
                continue
        if if_disabled:
            self.move_mouse_to_click_element(OrdersLocator.reset, doc)
        time.sleep(2)
        if if_choose == True:
            self.wait_elevisble(OrdersLocator.product, doc)
            product_list = self.get_elements(OrdersLocator.product, doc)
            for i in product_list:
                if i.text == product:
                    i.click()
                    break
            time.sleep(5)
        self.wait_elevisble(OrdersLocator.product_ok, doc)
        self.click_element(OrdersLocator.product_ok, doc)
        time.sleep(5)

    # 清空时间选项，选择当前时间
    def clear_time_select(self,if_now='1'):
        doc = '清空时间选项，选择当前时间'
        time.sleep(5)
        try:
            #self.wait_elevisble(OrdersLocator.order_time_clear,doc,timeout=10)
            self.move_mouse_to_click_element(OrdersLocator.order_time_clear,doc)
        except:
            pass
        try:
            self.wait_elevisble(OrdersLocator.wait_for_submit,doc,timeout=10)
            self.wait_eleinvisble(OrdersLocator.wait_for_submit,doc,timeout=10)
        except:
            pass
        if if_now =='1':
            self.wait_elevisble(OrdersLocator.order_time, doc)
            self.click_element(OrdersLocator.order_time, doc)
            time.sleep(2)
            self.wait_elevisble(OrdersLocator.order_select_time, doc)
            self.click_element(OrdersLocator.order_select_time, doc)
            self.click_element(OrdersLocator.order_select_time, doc)
        if if_now =='2':
            self.wait_elevisble(OrdersLocator.order_time, doc)
            self.click_element(OrdersLocator.order_time, doc)
            time.sleep(2)
            self.wait_elevisble(OrdersLocator.order_select_time_yesterday, doc)
            self.click_element(OrdersLocator.order_select_time_yesterday, doc)
            self.click_element(OrdersLocator.order_select_time_yesterday, doc)
        self.click_element(OrdersLocator.order_search, doc)
        time.sleep(5)
        self.save_screen(doc)


    # 重制查询条件
    def refresh_order_id(self):
        doc = '重置筛查询条件'
        time.sleep(2)
        self.wait_elevisble(OrdersLocator.order_id_input, doc)
        self.double_click(OrdersLocator.order_id_input, doc)
        self.keyboard_event(OrdersLocator.order_id_input, doc)
        time.sleep(1)
        self.move_mouse_to_click_element(OrdersLocator.order_search,doc)
        time.sleep(3)
        self.save_screen(doc)

    # 复原排序按钮
    def recover_create_time(self):
        doc='复原排序按钮'
        time.sleep(2)
        self.wait_elevisble(OrdersLocator.click_create_time,doc)
        self.move_mouse_to_element(OrdersLocator.click_create_time)
        for i in range(3):
            try:
                self.wait_elevisble(OrdersLocator.click_order,doc,timeout=5)
                break
            except:
                self.move_mouse_to_click_element(OrdersLocator.click_create_time,doc)
                time.sleep(5)
                continue


    # 检查order type=new的vm数据
    def check_first_data_detail(self,detail):
        doc = '查询第一条数据的order type状态'
        time.sleep(2)
        self.wait_elevisble(OrdersLocator.orders_select_num1,doc)
        orders_select_num1=self.get_element(OrdersLocator.orders_select_num1,doc).text
        self.wait_elevisble(OrdersLocator.order_type_select_num1,doc)
        order_type_select_num1=self.get_element(OrdersLocator.order_type_select_num1, doc).text
        text= '实际值:'+order_type_select_num1+'预期值:'+ detail
        assert order_type_select_num1 == detail, text


    # 根据order_id查询，点击下载进入下载页面
    def enter_down_load_page(self):
        doc = 'order进入下载页面'
        time.sleep(2)
        self.clear_time_select(if_now='2')
        self.refresh_order_id()
        time.sleep(2)
        try:
            self.wait_elevisble(OrdersLocator.total_items,doc,timeout=20)
            total_items=self.get_element(OrdersLocator.total_items,doc).text
            # 使用正则表达式模式对字符串进行拆分
            pattern = r"[ ]"  # 模式表示逗号、感叹号、问号或空格出现一次或多次
            result = re.split(pattern, total_items)
            total_num = int(result[1])
        except:
            total_num = 0
        time.sleep(2)
        self.move_mouse_to_click_element(OrdersLocator.order_search, doc)
        time.sleep(5)
        self.wait_elevisble(OrdersLocator.click_down,doc)
        self.move_mouse_to_click_element(OrdersLocator.click_down,doc)
        self.wait_elevisble(OrdersLocator.click_down_success_cue,doc)
        self.wait_eleinvisble(OrdersLocator.click_down_success_cue,doc)
        time.sleep(2)
        self.wait_elevisble(OrdersLocator.export_records_success,doc)
        return total_num

    """
        Export Records 
    """
    # 更新status状态，置空
    def update_export_status(self):
        doc = '更新Export Records页面的status状态为空'
        time.sleep(3)
        self.wait_elevisble(OrdersLocator.status_choose,doc)
        self.move_mouse_to_click_element(OrdersLocator.status_choose,doc)
        time.sleep(3)
        if self.is_element_disabled(OrdersLocator.reset_point):
            self.move_mouse_to_click_element(OrdersLocator.reset_point, doc)
            time.sleep(3)
            self.move_mouse_to_click_element(OrdersLocator.ok_point, doc)
        else:
            self.move_mouse_to_click_element(OrdersLocator.ok_point, doc)
            # self.wait_elevisble(OrdersLocator.ok_point, doc)
            # self.move_mouse_to_click_element(OrdersLocator.ok_point, doc)
        time.sleep(3)


    # 最新数据点击下载
    def check_download(self,cmdopt):
        doc = '点击下载后打开文件交验order_id'
        time.sleep(5)
        self.update_export_status()
        time.sleep(2)
        self.wait_elevisble(OrdersLocator.first_download,doc)
        self.move_mouse_to_click_element(OrdersLocator.first_download,doc)
        self.save_screen(doc+'-点击下载')
        self.wait_elevisble(OrdersLocator.first_down_load_data_name,doc)
        first_down_load_data_name=self.get_element(OrdersLocator.first_down_load_data_name,doc).text
        time.sleep(20)
        path=MenuName.download_path[cmdopt]
        download_path = path+first_down_load_data_name
        print('文件路径:',download_path)
        resultList, resultListlen=excel(download_path).getExcelData('Order ID')
        print(resultList, resultList)
        return resultList,resultListlen










